<?php

declare(strict_types=1);

namespace App\Controller\delivery;
use App\Controller\BaseController;
use App\Model\Payment;
use App\Model\RechargeGoods;
use App\Service\delivery\OrderService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\delivery\PaymentService;

#[AutoController]
class PaymentController  extends BaseController
{

    #[Inject]
    private PaymentService $PaymentService;
    #[Inject]
    private OrderService $orderService;
    /**
     * 订单列表
     */
    public function Index(RequestInterface $request)
    {
        $data = $this->PaymentService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);
    }
    public function backhaul(int $id)
    {
        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $paymentInfo = Payment::query()->select('id')->where(['id' => $id])->exists();
            if(empty($paymentInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '订单不存在');
            }
            $result = $this->PaymentService->backhaul($id);

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '上报订单成功');
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);
            }
        }

    }
    public function batchBackhaul(RequestInterface $request)
    {

        $result = $this->PaymentService->batchBackhaul($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '上报订单成功',$result['data']);
        }else{
            return $this->response($result['code'], $result['message'], $result['data']);
        }
    }

    /**
     * 获取订单详情
     */
    public function getOrderDetail(int $id){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else {
            $paymentInfo = Payment::query()->select(['id','type','status','applet_id','user_id','order_id','pay_type'])->where(['id' => $id,'status' => 2])->first();
            if(empty($paymentInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '订单不存在或者订单状态不正确');
            }
            $result = $this->orderService->getOrderDetail($paymentInfo);

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, $result['message'],$result['data']);
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);
            }
        }
    }

    //支付退款
    public function refundOrder(RequestInterface $request){

        $result = $this->orderService->refundOrder($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, $result['message'],$result['data']);
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);
        }

    }


    /**
     * 获取回传订单详情
     */
    public function backhaulInfo(int $id){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else {
            $paymentInfo = Payment::query()->select(['id'])->where(['id' => $id,'is_handle' => 'Y'])->first();
            if(empty($paymentInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '订单不存在或者订单状态不正确');
            }
            $result = $this->PaymentService->backhaulInfo($id);

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, $result['message'],$result['data']);
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);
            }
        }
    }

}